import { Component, OnInit } from '@angular/core';

// 引入服务
import {StorageService} from "../../services/storage.service";

@Component({
  selector: 'app-todolist',
  templateUrl: './todolist.component.html',
  styleUrls: ['./todolist.component.less']
})
export class TodolistComponent implements OnInit {
  public keyWord:string;
  public todoList:any[] = [];
  constructor(public storage:StorageService) {
  }

  ngOnInit() {
    console.log('todolist页面初始化');
    let todoList = this.storage.get('todoList');
    if(todoList){
      this.todoList = todoList;
    }
  }
  doAdd(e){
    if (e.keyCode === 13){
      if(this.todoListHasKeyWord(this.todoList,this.keyWord)){
        this.todoList.push({
          title:this.keyWord,
          status:0
        });
        this.storage.set('todoList',this.todoList);
      }
      this.keyWord = '';
    }
  }
  deleteDate(index){
    this.todoList.splice(index,1);
    this.storage.set('todoList',this.todoList);
  }
  todoListHasKeyWord(todoList:any,keyWord:any){
    if(keyWord == '') return false;
    for(let i=0;i<todoList.length;i++){
      if(todoList[i].title == keyWord){
        return false;
      }
    }
    return true;
  }
  checkBoxChange(){
    this.storage.set('todoList',this.todoList);
  }
}
